program mc_plates
  use rbf_type
  use variable
  use ifport
  !  Code for study LC confined between plates, based on the framework of the continuum model (MC approach).
  ! in this new code, we can study the effect to include a nanoparticle inside of the system
  ! The nanoparticle can have homeotropic or planar anchoring, also, it is possible to study
  ! the Janus particle case (half-half)

  implicit none
 integer :: nspin
  real(dp), dimension(8) :: actual_energy
  real (dp) :: E,dE,delta_t
!--------------------------------------------------------------------------
  real (dp) :: clock_start,clock_finish,c_ini,c_fin
!--------------------------------------------------------------------------
  save E 
  
  !INOUT data
  call cpu_time(clock_start)
  call names
  call inout


  !--> --> FREE ENERGY OF THE ACTUAL SYSTEM <-- <--
  call free_energy( actual_energy  )
  c_ini=0.d0 ;  c_fin=0.d0  ; call cpu_time (c_ini)
  E = 1.d-15 ; dE=0.d0
  temp_ini = 10.d0 ; temp_fin = 1.d15

  delta_t=(log(temp_fin)-log(temp_ini))/((timesteps-ini_step)/(copia))
!------------------------------------------------------------------------------------
!     Ginzburg-Landau Evolution
!------------------------------------------------------------------------------------

  cycles: do while ( step <= timesteps )

     if(mod(step,100).eq.0)then
        call initial_da  ; call free_energy( energy ) ;            
        write(20,120)step,energy 
     endif   


     if(mod(step,copia).eq.0)then
        invtemp = exp(log(temp_ini) + delta_t*((step-ini_step)/(copia)))
        write(430,*)step,invtemp
     endif

     if ( mod(step,copia) == 0) then

        call adjust
        call initial_da  ; call free_energy( energy ) ; call cpu_time (c_fin)           
        write(6,999)step,timesteps,(c_fin-c_ini)/60.d0,(c_fin-clock_start)/60.d0,dE
        write(6,120)step,energy 
        c_ini=0.d0 ; c_fin=0.d0  ; call cpu_time (c_ini)
 
     endif


     if ( mod(step,copia*10) == 0) then
       call output
        call qmga               (step)
    !    call paraview_eigen_new(step)
     endif
     moves: do nspin=1,nodes
        call mc_moves(actual_energy)
     end do moves


!     call mcmoves_chiral

   
    step= step + 1
enddo cycles

  ! files
close(500)
 
  ! Memory
!  deallocate( xm, normal, q, a, da, chi )


  print *,' '
  print *,' NORMAL TERMINATION'

120 format (i8,8(1x,E16.8))
999 format ('   Diffusion Time:',I10,2x,I10,3x,'Time for each cycle  :',f8.4,' min',', Total CPU time:',f10.4,' min','   dE',e12.3)


600  call cpu_time (clock_finish)
  call timer(clock_finish,clock_start)
  stop

end program mc_plates
!---------------------------------------------------------------
!---------------------------------------------------------------
!---------------------------------------------------------------

subroutine timer(clock_finish,clock_start)
  double precision :: clock_finish,clock_start
  if((clock_finish-clock_start)/60 < 60.d0)then
     write (*,999)(clock_finish-clock_start)/60.d0
  else
     write (*,998)(clock_finish-clock_start)/3600.d0
  endif
999 format ('Total time of CPU                 :',f26.10,'  minutes')
998 format ('Total time of CPU                 :',f26.10,'  hours')
end subroutine timer


subroutine timer1(clock_finish,clock_start)
  double precision :: clock_finish,clock_start
!  if((clock_finish-clock_start)/60 < 60.d0)then
     write (*,999)(clock_finish-clock_start)
!  else
!     write (*,998)(clock_finish-clock_start)/3600.d0
!  endif
999 format ('Total time of CPU                 :',f36.20,'  seconds')
998 format ('Total time of CPU                 :',f36.20,'  hours')
end subroutine timer1
